package bookStore.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookStore.service.business.BookService;
import bookStore.service.business.BookServiceImpl;
import bookStore.tool.Constants;
import bookStore.tool.UrlUtil;
import bookStore.vo.BookVo;
import bookStore.vo.BookWithPicture;

/**
 * Servlet implementation class BookController
 */
@WebServlet("/book/*")
public class BookController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String url;
	private String traget="";
	private BookService bookService = new BookServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		url = UrlUtil.getUrl(request.getRequestURI());
		switch(url) {
			case "allBooksForIndex":
				int offset = Integer.parseInt(request.getParameter("pager.offset"));
				
				List<BookWithPicture> newestBooks = bookService.queryBookWithPictureByIdDesc(offset, Constants.PAGE_SIZE);
				List<BookWithPicture> highScoreBooks = bookService.queryBookWithPictureByScore(offset, Constants.PAGE_SIZE);
				request.setAttribute("newestBooks", newestBooks);
				request.setAttribute("highScoreBooks", highScoreBooks);
				setTraget("/WEB-INF/business/book/list.jsp");
				break;
			case "detail":
				int bookId = Integer.parseInt(request.getParameter("bookId"));
				BookVo bookVo = bookService.getBookVoByBookId(bookId);
				request.setAttribute("book", bookVo);
				setTraget("/WEB-INF/business/book/item.jsp");
				break;
			//以下为注册用户专享
			//根据书名或作者查询
			case "queryBookByAuthorOrTitle":
				String condition = request.getParameter("condition");
				if(condition==null) {
					condition="";
				}
				List<BookWithPicture> books = bookService.queryBookWithPictureByAuthorOrTitle(condition, Integer.parseInt(request.getParameter("pager.offset")), Integer.parseInt(request.getParameter("pageSize")));
				request.setAttribute("books", books);
				long count= bookService.CountBookWithPictureByAuthorOrTitle(condition);
				request.setAttribute("total", count);
				setTraget("/WEB-INF/business/book/queryList.jsp");
				break;
			//对某书打分并评论
			case "addReview":
				break;
			//查询自己打分的图书
			case "queryBookWithPictureByUser":
				break;
		}
		
		if(traget.endsWith(".jsp")) {
			request.getRequestDispatcher(traget).forward(request, response);
		}else {
			response.sendRedirect(traget);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public String getTraget() {
		return traget;
	}

	public void setTraget(String traget) {
		this.traget = traget;
	}

}
